package com.shigen.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shigen.demo.bean.Result;
import com.shigen.demo.entity.BaseUserFile;
import com.shigen.demo.entity.req.BaseUserFileReq.QueryByFolder;
import com.shigen.demo.entity.req.BaseUserFileReq.Rename;
import com.shigen.demo.entity.req.FileFolderReq;
import com.shigen.demo.entity.req.FileFolderReq.MoveReq;
import com.shigen.demo.entity.vo.FileNodeVo;
import com.shigen.demo.service.IBaseUserFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "BaseUserFile")
@Api(tags = "用户文件")
public class BaseUserFileController {

  @Resource
  private IBaseUserFileService baseUserFileService;

  @GetMapping(value = "getTree")
  @ApiOperation(value = "获得文件树")
  public Result<List<FileNodeVo>> tree() {
    return Result.ok(baseUserFileService.listWithTree());
  }

  @GetMapping(value = "getPath/{unionId}")
  @ApiOperation(value = "通过文件的unionId获得文件的访问地址")
  public Result<String> findByUnionId(@PathVariable("unionId") String unionId) {
    return Result.ok(baseUserFileService.findByUnionId(unionId));
  }

  @PostMapping(value = "createFolder")
  @ApiOperation(value = "创建一个文件夹")
  public Result<FileNodeVo> createFolder(@RequestBody @Valid FileFolderReq.CreateFolderReq createFolderReq) {
    return Result.ok(baseUserFileService.createFolder(createFolderReq));
  }

  @PostMapping(value = "upload")
  @ApiOperation(value = "文件的批量上传")
  public Result<String> upload(@RequestParam("files") MultipartFile[] files, @RequestParam("parentId") String parentId) {
    int upload = baseUserFileService.uploadFiles(files, parentId);
    return upload > 0 ? Result.ok("已成功上传 " + upload + " 个文件") : Result.err("上传失败");
  }

  @PostMapping(value = "move")
  @ApiOperation(value = "文件或文件夹的移动")
  public Result<String> move(@RequestBody @Valid MoveReq moveReq) {
    boolean move = baseUserFileService.move(moveReq.getUnionId(), moveReq.getParentId());
    return Result.ok("移动成功");
  }

  @DeleteMapping(value = "delete")
  @ApiOperation(value = "根据文件的id批量删除")
  public Result<String> delBatchByUnionId(@RequestBody ArrayList<String> unionIds) {
    boolean delete = baseUserFileService.delBatchByUnionId(unionIds);
    return delete ? Result.ok("已成功删除 " + unionIds.size() + " 个文件") : Result.err("删除失败");
  }

  @PostMapping(value = "folderList")
  @ApiOperation(value = "查询文件夹下的文件")
  public Result<IPage<BaseUserFile>> getFolderFilesByPage(@RequestBody @Valid QueryByFolder queryByFolder) {
    return Result.ok(baseUserFileService.getFolderFilesByPage(queryByFolder));
  }

  @GetMapping(value = "get/{id}")
  @ApiOperation(value = "获得单条文件的详情")
  public Result<BaseUserFile> getFileDetail(@PathVariable("id") Long id) {
    return Result.ok(baseUserFileService.getFileDetail(id));
  }

  @PutMapping(value = "rename")
  @ApiOperation(value = "更新文件名")
  public Result<String> reNameFile(@RequestBody @Valid Rename rename) {
    int update = baseUserFileService.reNameFile(rename.getId(), rename.getNewName());
    return update > 0 ? Result.ok("重命名成功") : Result.err("重命名失败");
  }
}
